package com.ziki.test.socket.util;

import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Log {
    public Log() {

    }

    public static void write2Log(int logLevel, String fileName, String module, String msg) {
        String str = getFormatLog(module, msg);
        System.out.println(str);
        write2LogFile(fileName, str);
    }

    public static void write2Log(int logLevel, String module, String msg) {
        String str = getFormatLog(module, msg);
        System.out.println(str);
        write2LogFile(null, str);
    }

    public synchronized static void write2LogFile(String fileName, String str) {
        try {
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
            String directory = System.getProperty("user.dir") + "/log/";
            File dir = new File(directory);
            if (!dir.isDirectory()) {
                dir.mkdir();
            }
            if (fileName == null || "".equals(fileName)) {
                fileName = directory + sdf1.format(new Date()) + ".txt";
                File file = new File(fileName);
                if (file.exists()) {
                    if (file.length() > 1024 * 1024) {
                        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd_HHmm");
                        String fileName2 = directory + sdf2.format(new Date()) + ".txt";
                        file.renameTo(new File(fileName2));
                    }
                }
                else {
                    file.createNewFile();
                }
            }
            else {
                fileName = directory + fileName;
                File file = new File(fileName);
                if (!file.exists()) {
                    file.createNewFile();
                }
            }

            FileWriter fileWriter = new FileWriter(fileName, true);
            fileWriter.write(str);
            fileWriter.flush();
            fileWriter.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static String getFormatLog(String module, String msg) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        StringBuffer sb = new StringBuffer();
        sb.append("【");
        sb.append(sdf.format(new Date()));
        sb.append("】");
        sb.append("【" + module + "】");
        sb.append(msg);
        if (!msg.endsWith("\r\n")) {
            sb.append(System.getProperty("line.separator"));
        }
        return sb.toString();
    }

    public static void printDataLog(String module, byte[] data) {
        try {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < data.length; i++) {
                String temp = Integer.toHexString(data[i]).toUpperCase();
                if (data[i] < 0) {
                    temp = temp.substring(temp.length() - 2, temp.length());
                }
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                sb.append(temp);
                sb.append(" ");
                if ((i % 16 == 15) || (i == data.length - 1)) {
                    sb.append(System.getProperty("line.separator"));
                }
            }
            Log.write2Log(1, module, "\r\n" + sb.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void writeLog(String module, String msg) {
        String str = getFormatLog(module, msg);
        System.out.println(str);
        write2LogFile(null, str);
    }
}
